<!DOCTYPE html>
<html>
<head>
<#include "../common/head.ftl">
</head>
<body>
<div class="wrapper">
    <div class="section">
        <div class="search-wrapper clearfix">
            <div class="pull-left">
                <form class="form-inline" id="search_form">
                    <div class="form-group">
                        <input type="text" name="keywords" class="form-control" placeholder="请输入名称或标识" maxlength="20">
                        <input type="hidden" name="pid" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>类型：</label>
                        <select class="form-control w100" name="type" >
                            <option value="">-请选择-</option>
                            <@sysDictTag  namespace="workflow_type">
                                <#list noteDicts as noteDict>
                                    <option value="${noteDict.value}">${noteDict.label}</option>
                                </#list>
                            </@sysDictTag>
                        </select>
                    </div>
                    <button type="button" class="btn btn-default search-btn"><span class="fa fa-search"></span>&nbsp;查询</button>
                    <button type="reset" class="btn btn-default"><span class="fa fa-undo"></span>&nbsp;重置</button>
                </form>
            </div>
            <div class="pull-right">
                <button type="button" id="add_btn" class="btn btn-default"><span class="fa fa-plus"></span>&nbsp;用户组设置</button>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-10">
                <div class="grid-wrapper">
                    <table id="grid"></table>
                    <div id="pager"></div>
                </div>
            </div>
            <div class="col-sm-2">
                <div class="panel panel-default">
                    <div class="panel-heading">快速搜索</div>
                    <div class="panel-body" style="height:358px;overflow:auto">
                        <ul id="p_sys_res_tree" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<#include "../common/tab_oper.ftl">
</body>
<#include "../common/scripts.ftl">
<script type="text/javascript">
    var grid,curNode,tree;
    tree = $.fn.zTree.init($("#p_sys_res_tree"), {
        async : {
            autoParam:["id"],
            enable : true,
            url : "${ctx}/sys_resource/list/tree"
        },
        data : {
            simpleData : {
                enable : true,
                idKey : "id",
                pIdKey : "pid"
            }
        },
        callback: {
            onClick: function(e, treeId, treeNode){
                if(treeNode==curNode) {
                    $("#search_form input[name='pid']").val("");
                    tree.cancelSelectedNode(treeNode);
                    curNode = null;
                    $(".search-btn").trigger("click");
                } else {
                    $("#search_form input[name='pid']").val(treeNode.id);
                    curNode = treeNode;
                    $(".search-btn").trigger("click");
                }
            }
        }
    });

    grid = $("#grid").jqGrid({
        height: Public._win.gridBigHeight(),
        url: "sys_resource/list/page",
        pager: "#pager",
        colModel: [
            {label:"操作",name:"ctrl",width:20,formatter:function(val,opt,obj){
                    var html = "<div class='operating'>";
                    html += "<shiro:hasPermission name='sys_resource:edit'><span class='fa fa-edit edit_btn' title='修改' data-id='"+obj.m.id+"'></span>&nbsp;&nbsp;</shiro:hasPermission>";
                    html += "<shiro:hasPermission name='sys_resource:delete'><span class='fa fa-trash delete_btn' title='删除' data-id='"+obj.m.id+"'></span></shiro:hasPermission>";
                    html += "</div>";
                    return html;
                }},
            {label:"ID",name:"m.id",key:true,hidden:true},
            {label:"名称",name:"m.name",width:40},
            {label:"类型",name:"m.type",width:20,formatter:function(val){return val==0?"<span class='text-primary'>菜单</span>":"<span class='text-success'>资源</span>";}},
            {label:"层级",name:"m.level",width:20},
            {label:"图标",name:"m.icon",width:40,formatter:function(val,opt,obj){return val?("<span class='"+val+"'></span>&nbsp;（"+val+"）"):"";}},
            {label:"URL",name:"m.link",width:60},
            {label:"权限标识",name:"m.permission",width:40},
            {label:"排序",name:"m.priority",width:20}
        ],
        loadComplete : function() {
            $(".edit_btn").on("click",function() {
                var id = $(this).data("id");
                layer.open({
                    type : 2,
                    maxmin:true,
                    title : "修改系统资源信息",
                    area : [ "768px", "350px" ],
                    content : "sys_resource/edit/"+id
                });
            });
            $(".delete_btn").on("click",function(){
                var id = $(this).data("id");
                layer.confirm("确认删除该资源信息？", {
                    btn: ["确认","取消"]
                }, function(index,layero){
                    $(layero).find(".layui-layer-btn0").off("click");
                    var noCache = new Date().getTime();
                    $.getJSON("sys_resource/delete/" + id,{"noCache":noCache}, function(data) {
                        layer.msg(data.message);
                        grid.jqGrid().trigger("reloadGrid");
                        tree.reAsyncChildNodes(null, "refresh");
                    });
                }, function(){});
            });
        },
        gridComplete : function() {
            setTimeout(function() {
                Public._grid.autoSize(grid);
            }, 10);
        }
    });
    $(".search-btn").on("click",function(){
        grid.jqGrid("setGridParam",{postData:$("#search_form").serializeObject(),page:1}).trigger("reloadGrid");
    });

    $("#add_btn").on("click",function(){
        layer.open({
            type : 2,
            title : "新增系统资源信息",
            area : [ "768px", "320px" ],
            content : "sys_resource/add"
        });
    });
</script>
</html>